package com.toy.core.util;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

import org.apache.commons.lang.time.DateFormatUtils;

/**
 * @author Jiffy
 */

public class DateUtils {
	
	public static SimpleDateFormat basic = new SimpleDateFormat("yyyy-MM-dd");
	
	public static SimpleDateFormat detail = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	
	public static final long SECOND_LONG = 1000;

	public static final long MINUTE_LONG = 1000 * 60;

	public static final long HOUR_LONG = 1000 * 60 * 60;

	public static final long DAY_LONG = 1000 * 60 * 60 * 24;

	public static String currentDate() {
		return DateFormatUtils.ISO_DATE_FORMAT.format(new Date());
	}

	public static String currentTime() {
		return DateFormatUtils.ISO_DATETIME_FORMAT.format(new Date());
	}

	public static Timestamp currentTimestamp() {
		return new Timestamp(System.currentTimeMillis());
	}

	public static Timestamp nextTimestamp(long microsecond) {
		return new Timestamp(System.currentTimeMillis() + microsecond);
	}

	public static Timestamp perTimestamp(long microsecond) {
		return new Timestamp(System.currentTimeMillis() - microsecond);
	}

	/**
	 * 
	 * @return
	 */
	public static Date todayEndDate() {
		Date date = new Date(System.currentTimeMillis() + DAY_LONG);
		String str = DateFormatUtils.ISO_DATE_FORMAT.format(date);
		SimpleDateFormat format = new SimpleDateFormat(
				DateFormatUtils.ISO_DATE_FORMAT.getPattern());

		try {
			Date todayEndDate = format.parse(str);
			todayEndDate.setTime(todayEndDate.getTime() - 1);
			return todayEndDate;
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static Date todayBeginDate() {
		String str = DateFormatUtils.ISO_DATE_FORMAT.format(new Date());
		SimpleDateFormat format = new SimpleDateFormat(
				DateFormatUtils.ISO_DATE_FORMAT.getPattern());
		try {
			Date todayEndDate = format.parse(str);
			return todayEndDate;
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 现在是星期几
	 * 
	 * @return
	 */
	public static int currentDayOfWeek() {
		Calendar c = Calendar.getInstance();
		c.setTime(new Date());
		int dayForWeek = 0;
		if (c.get(Calendar.DAY_OF_WEEK) == 1) {
			dayForWeek = 7;
		} else {
			dayForWeek = c.get(Calendar.DAY_OF_WEEK) - 1;
		}
		return dayForWeek;
	}
	
	/**
	 * 获得本周星期几对应的日期
	 * @param n
	 * @return
	 */
	public static String getDateOfWeek(int n){
		int today = currentDayOfWeek();
		Calendar c = Calendar.getInstance();
		c.clear();
		c.setTime(new Date());
		c.add(Calendar.DATE, n-today);
		return basic.format(c.getTime());
	}
	
	/**
	 * 获得某一天的开始和结束时间
	 * @param date
	 * @return
	 */
	public static Date[] getDayRange(String date){
		try {
			Date beginDate = basic.parse(date);
			long time = beginDate.getTime();
			time = time + 24*60*60*1000 - 1;
			return new Date[]{beginDate,new Date(time)};
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 距离今天的时间范围
	 * @return
	 */
	public static Date[] getAwayNowRange(int days){
		Date now = new Date();
		Calendar c = Calendar.getInstance();
		c.setTime(now);
		int dayOfYear = c.get(Calendar.DAY_OF_YEAR);
		dayOfYear = dayOfYear - days ;
		c.set(Calendar.DAY_OF_YEAR, dayOfYear);
		return new Date[]{c.getTime(),now };
	}
	
	private static final Map<String,String> WEEKMAPPER = new HashMap<String,String>(7);
	
	static {
		WEEKMAPPER.put("1", "星期一");
		WEEKMAPPER.put("2", "星期二");
		WEEKMAPPER.put("3", "星期三");
		WEEKMAPPER.put("4", "星期四");
		WEEKMAPPER.put("5", "星期五");
		WEEKMAPPER.put("6", "星期六");
		WEEKMAPPER.put("7", "星期日");
	}
	
	/**
	 * 获得本周内前几天日期对应星期几
	 * @return
	 */
	public static Map<String,String> getPreDaysOfWeek(){
		int today = currentDayOfWeek();
		if (today ==1 ) return Collections.emptyMap();
		Map<String,String> days = new LinkedHashMap<String,String>();
		for (int i = today -1  ; i >=1; i--) {
			if ( i == today -1){
				days.put(getDateOfWeek(i), "昨天");
			}else{
				days.put(getDateOfWeek(i), WEEKMAPPER.get(i+""));
			}
		}
		return days;
	}
	
	/**
	 * 获得上周的时间范围
	 */
	public static Date[] getLastWeekRange(){
		try {
			Date todayBegin = basic.parse(basic.format(new Date()));
			long todayBeginTime = todayBegin.getTime();
			int weekday = DateUtils.currentDayOfWeek();
			long lastWeekBegin = todayBeginTime - (weekday + 6) * 3600 * 1000* 24;
			long lastWeekEnd = todayBeginTime - (weekday - 1) * 3600 * 1000* 24 - 1000;
			return new Date[]{new Date(lastWeekBegin),new Date(lastWeekEnd)};
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 获得本周的世界范围
	 */
	public static Date[] getThisWeekRange(){
		String stateDate = getDateOfWeek(1);
		Date[] stateDateRange = getDayRange(stateDate);
		return new Date[]{stateDateRange[0],new Date()};
	}
	
	/**
	 * 获得更早的时间[指上周之前]
	 * @return
	 */
	public static Date getMoreEarlyDate(){
		try {
			Date todayBegin = basic.parse(basic.format(new Date()));
			long todayBeginTime = todayBegin.getTime();
			int weekday = DateUtils.currentDayOfWeek();
			long lastWeekBegin = todayBeginTime - (weekday + 6) * 3600 * 1000* 24;
			return new Date(lastWeekBegin - 1000);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 获得今天的时间范围
	 */
	public static Date[] getTodayRange(){
		try {
			Date now = new Date();
			Date todayBegin = basic.parse(basic.format(now));
			return new Date[]{todayBegin,now};
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static void main(String[] args) {
		try {
			SimpleDateFormat basic = new SimpleDateFormat("yyyy-MM-dd");
			SimpleDateFormat detail = new SimpleDateFormat(
					"yyyy-MM-dd HH:mm:ss");
			Date today = new Date();
			Date todayBegin = basic.parse(basic.format(today));
			long todayBeginTime = todayBegin.getTime();

			System.out.println("today begin==>" + detail.format(todayBegin));
			System.out.println("today end==>" + detail.format(today));

			long yesterdayBegin = todayBeginTime - 24 * 3600 * 1000;
			long yesterdayEnd = todayBeginTime - 1000;

			System.out.println("yesterday begin==>"
					+ detail.format(new Date(yesterdayBegin)));
			System.out.println("yesterday end==>"
					+ detail.format(new Date(yesterdayEnd)));
//
			int weekday = DateUtils.currentDayOfWeek();
			System.out.println("weekday==》" + weekday);
			long lastWeekBegin = todayBeginTime - (weekday + 6) * 3600 * 1000
					* 24;
			long lastWeekEnd = todayBeginTime - (weekday - 1) * 3600 * 1000
					* 24 - 1000;
			System.out.println("lastWeek begin==>"
					+ detail.format(new Date(lastWeekBegin)));
			System.out.println("lastWeek end==>"
					+ detail.format(new Date(lastWeekEnd)));
			
			Calendar c = Calendar.getInstance();
			c.clear();

			c.setTime(new Date());  
			c.setFirstDayOfWeek(Calendar.MONDAY);
			System.out.println("c.get(Calendar.DAY_OF_WEEK)==>"+c.get(Calendar.DAY_OF_WEEK));
			c.add(Calendar.DATE, 0);
			System.out.println("+1==>"+detail.format(c.getTime()));
			System.out.println(DateUtils.getDateOfWeek(2));
			Date[] range = DateUtils.getDayRange(DateUtils.getDateOfWeek(3));
			System.out.println(detail.format(range[0]));
			System.out.println(detail.format(range[1]));
			System.out.println(getPreDaysOfWeek());
			
			range = getAwayNowRange(365);
			System.out.println(detail.format(range[0]));
			System.out.println(detail.format(range[1]));
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
}

